<template>
  <div>
    <a-modal title="设置发货仓" :visible="visible" @before-open="open" @before-ok="handleOk" @cancel="handleCancel" :align-center="false" title-align="start" width="400px" :mask-closable="false">
      <div>
        <a-form auto-label-width ref="formRef" size="medium" :model="form">
          <a-space direction="vertical" :size="24">
            <a-row>
              <a-col :span="24">
                <a-form-item field="warehouseCode" label="选择发货仓" :rules="rules.warehouseCode">
                  <a-select v-model="form.warehouseCode" allow-clear placeholder="请选择" allow-search :fallback-option="() => ''">
                    <a-option v-for="item of houseList" :value="item.warehouseCode" :label="item.warehouseName"></a-option>
                  </a-select>
                </a-form-item>
              </a-col>
            </a-row>
          </a-space>
        </a-form>
      </div>

      <template #footer>
        <a-button class="gray-outline" @click="handleCancel">取消</a-button>
        <a-button type="primary" :loading="loading" @click="handleOk">确定</a-button>
      </template>
    </a-modal>
  </div>
</template>

<script lang="ts">
  import { defineComponent, ref, reactive, toRefs, computed } from 'vue'
  import { findWarehouseListByParam } from '@/api/warehouse'
  import { updatePackageWarehouse, updateOrderPackageWarehouse } from '@/api/order'
  import { Message } from '@arco-design/web-vue'
  export default defineComponent({
    props: ['selectRow', 'type'],
    setup(props: any, { emit }) {
      let visible = ref(false)
      const formRef = ref()
      const data: any = reactive({
        form: {},
        rules: {
          warehouseCode: [{ required: true, message: '请选择发货仓' }],
        },
        loading: false,
        houseList: [],
      })

      const open = () => {
        getWareHouseList()
        console.log(props.type)
      }
      const handleOk = () => {
        formRef.value.validate(async (valid: any) => {
          if (!valid) {
            let params: any = {
              warehouseCode: data.form.warehouseCode,
              warehouseName: data.houseList.find((e: any) => e.warehouseCode == data.form.warehouseCode).warehouseName,
            }
            let res: any = {}
            data.loading = true
            if (props.type != 'order') {
              params.orderPackageIdList = props.selectRow.map((e: any) => e.id)
              res = await updatePackageWarehouse(params)
            } else {
              params.orderIdList = props.selectRow.map((e: any) => e.id)
              res = await updateOrderPackageWarehouse(params)
            }

            if (res.code == 'SUCCESS') {
              data.form = {}
              Message.success('修改发货仓成功')
              visible.value = false
              emit('save')
            }
            data.loading = false
          }
        })
      }

      const getWareHouseList = async () => {
        const res = await findWarehouseListByParam()
        data.houseList = res?.data
      }

      const handleCancel = () => {
        data.form = {}
        visible.value = false
      }
      return {
        ...(toRefs(data) as any),
        visible,
        formRef,
        open,
        handleOk,
        handleCancel,
      }
    },
  })
</script>

<style lang="less" scoped></style>
